package logic

import (
	"context"
	"errors"
	"shuqi-novel/models"

	"shuqi-novel/novelrpc/internal/svc"
	"shuqi-novel/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 登录
func (l *LoginLogic) Login(in *novelrpc.LoginRequest) (*novelrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line

	var user models.User
	if err := user.UserFindById(models.DB, int64(user.ID)); err != nil {
		return nil, errors.New("用户不存在")
	}

	if err := user.UserLogin(models.DB, in.Mobile, in.Password); err != nil {
		return nil, errors.New("账号或者密码错误")
	}

	return &novelrpc.LoginResponse{
		UserId: int64(user.ID),
	}, nil
}
